package com.example.himalayaproject.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.himalayaproject.R;
import com.example.himalayaproject.bean.Image;
import com.example.himalayaproject.bean.ImageEntity;
import com.example.himalayaproject.callback.ImageService;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FirstStartActivity extends AppCompatActivity implements Callback<ImageEntity> {
    private SharedPreferences preferences;
    private ImageView imageView;
    private String imageUrl;
    private RelativeLayout relativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_start);
        preferences = getSharedPreferences("app-config", Context.MODE_PRIVATE);
        imageView = (ImageView) findViewById(R.id.activity_firstStart_image);
        relativeLayout = (RelativeLayout) findViewById(R.id.activity_firstStart_relativelayout);
        new StartThread().start();
    }

    private void aboutImage() {
        ImageService service = new Retrofit.Builder()
                .baseUrl("http://adse.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(ImageService.class);
        service.getImage().enqueue(this);
    }

    @Override
    public void onResponse(Call<ImageEntity> call, Response<ImageEntity> response) {
        imageUrl = response.body().getData().get(0).getCover();
        Log.d("1630", imageUrl + 1111);
        Picasso.with(this).load(imageUrl).resize(relativeLayout.getWidth(), relativeLayout.getHeight()).into(imageView);
    }

    @Override
    public void onFailure(Call<ImageEntity> call, Throwable t) {

    }

    private class StartThread extends Thread {
        @Override
        public void run() {
            super.run();
            Intent intent = new Intent();
            boolean isFirstLogin = preferences.getBoolean("isFirstLogin", true);
            if (isFirstLogin) {
                imageView.setVisibility(View.GONE);
                intent.setClass(FirstStartActivity.this, WelcomeActivity.class);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putBoolean("isFirstLogin", false);
                editor.commit();
            } else {
                aboutImage();
                imageView.setVisibility(View.VISIBLE);
                try {
                    Thread.sleep(5 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                intent.setClass(FirstStartActivity.this, MainActivity.class);
            }
            startActivity(intent);
            finish();
        }
    }


}
